package net.lzzy.practices.wed.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.wed.models.ApiResult;
import net.lzzy.practices.wed.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "管理员业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_USER_ROOT)
public class UserApiController {
        private final UserService service;

        @Autowired
        public UserApiController(UserService service){
            this.service = service;
        }

        @ApiOperation(value = "获取所有用户", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;token(String)<br/>")
        @GetMapping(ApiConstants.ROUTE_USER_ALL)
        public ApiResult getUsers(@RequestParam String token){
//            if (AuthUtils.illegalUser(token,new int[]{User.TYPE_ADMIN})){
//                return new ApiResult(false, Messages.NOT_MARSHALLABLE.toString(),null);
//            }
            List<VmUser> users = new ArrayList<>();
            service.get().forEach(user -> users.add(VmUser.create(user,true)));
            return new ApiResult(true,users.size() + "",users);
        }

    @ApiOperation(value = "分页获取所有用户", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.page(int)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.page(int)<br/>&nbsp;&nbsp;&nbsp;&nbsp;3.token(String)<br/>")
        @GetMapping(ApiConstants.ROUTE_USER_PAGE)
        public ApiResult getPagedUsers(@RequestParam String token,@RequestParam int page,@RequestParam int size){
//            if (AuthUtils.illegalUser(token,new int[]{User.TYPE_ADMIN})){
//                return new ApiResult(false, Messages.NOT_MARSHALLABLE.toString(),null);
//            }
            List<VmUser> users = new ArrayList<>();
            service.get(page,size).forEach(user -> users.add(VmUser.create(user,true)));
            return new ApiResult(true,service.count() + "",users);
        }

    @ApiOperation(value = "根据关键词搜索用户", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.kw(String)<br/>")
        @GetMapping(ApiConstants.ROUTE_USER_SEARCH)
        public ApiResult search(@RequestParam String token,@PathVariable String kw){
            List<VmUser> users = new ArrayList<>();
            service.search(kw).forEach(user ->users.add(VmUser.create(user,true)));
            return new ApiResult(true,users.size() + "",users);
        }

    @ApiOperation(value = "获取申请审批教师用户", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.applying(boolean)<br/>")
        @GetMapping(ApiConstants.ROUTE_USER_APPLYING)
        public ApiResult getApplyingUsers(@RequestParam String token){
            List<VmUser> users = new ArrayList<>();
            service.getApplying(true).forEach(user ->users.add(VmUser.create(user,true)));
            return new ApiResult(true,users.size() + "",users);
        }

    @ApiOperation(value = "审批教师申请", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.id<UUID><br/>")
        @PostMapping(ApiConstants.ROUTE_USER_APPROVE)
        public ApiResult approve(@RequestParam String token,@RequestParam UUID id){
            User user = service.approveTeacher(id);
            if (user == null){
                return new ApiResult(false, Messages.WRONG_ID.toString(),null);
            }
            return new ApiResult(true,"",VmUser.create(user,true));
        }

    @ApiOperation(value = "驳回审批教师申请", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.id<UUID><br/>")
    @PostMapping(ApiConstants.ROUTE_USER_DECLING)
    public ApiResult decide(@RequestParam String token,@RequestParam UUID id){
        User user = service.declineTeacher(id);
        if (user == null){
            return new ApiResult(false,Messages.WRONG_ID.toString(),null);
        }
        return new ApiResult(true,"",VmUser.create(user,true));
    }

    @ApiOperation(value = "禁止用户登录", notes = "<b>参数列表：</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;1.token(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.id<UUID><br/>")
    @PostMapping(ApiConstants.ROUTE_USER_BAN)
    public ApiResult ban(@RequestParam String token,@RequestParam UUID id){
        User user = service.banUser(id);
        if (user == null){
            return new ApiResult(false,Messages.WRONG_ID.toString(),null);
        }
        return new ApiResult(true,"",VmUser.create(user,true));
    }
}
